#!/bin/sh
# 
# Munipack frontend.
#
##########################################################################
#
# Global variables
#
BIN=/usr/local/bin
#
FITS="*.fits"
#
SIM=1
#
FILTER=
#
###########################################################################
#
# Functions
#

function konve 
{
echo
echo -n "Images to convert: "
read -e N

O=`echo $N | awk '{ split($0,a,"."); print a[1]; }'`
O="$O.fits"

#echo $O

A=""

X=0
while test $X -ne 7 ; do

echo "1 ... give the parameter file"
echo "2 ... output mask (default=$O)"
echo "3 ... counter initial value (default = 1)"
echo "4 ... change flipping around axis "
echo "5 ... create only header of FITS file"
echo "6 ... time correction in seconds"
echo "7 ... continue"
echo -n "What? "
read X

case $X in
      1)   echo -n "name="; read B; A="$A -p $B";; 
      2)   echo -n "mask="; read O;;
      3)   echo -n "initial counter="; read B; A="$A -c $B";;
      4)   echo -n "what axis? x,y or xy "; read B; A="$A -flip $B";;
      5)   A="$A -h "; echo "only heades...";;
      6)   echo -n "time correction in seconds="; read B; A="$A tcor=$B";;
esac  

done

if [ "$SIM" -eq "1" ]; then
   echo "ls $N | nice $BIN/konve @ \"mask=$O $A\"" >> processing.sh
else
   ls $N | nice $BIN/konve @ "mask=$O $A"
fi
FITS=$O
} 

#########################################################################

function dark 
{
echo
echo -n "Images to dark subtraction: (default=$FITS) "
read N

if [ -z "$N" ]; then   
   N=$FITS;  
fi

echo -n "dark image: "
read D

echo -n "Output image: (default=overwrite input) "
read O

if [ -z "$O" ]; then    
   O="."; 
fi

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/darkbat @ \"dark=$D mask=$O\"" >> processing.sh
else
   ls $N | nice $BIN/darkbat @ dark=$D "mask=$O"
fi
}

#########################################################################

function flat 
{
echo
echo -n "Images to flat-fielld correction: (default=$FITS) "
read N

if [ -z "$N" ]; then
    N=$FITS
fi

echo -n "flat image: "
read D

echo -n "Output image (default=overwrite input): "
read O

if [ -z "$O" ]; then
    O="."
fi

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/flatbat @ flat=$D \"mask=$O\"" >> processing.sh
else
   ls $N | nice $BIN/flatbat @ flat=$D "mask=$O"
fi
}

#########################################################################

function mean 
{
echo
echo -n "Images to mean: "
read N

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/mdark " >> processing.sh
else
   ls $N | nice $BIN/mdark 
fi
}

#########################################################################

function autoflat 
{
echo
echo -n "Images to create flat: (default=$FITS) "
read N

if [ -z "$N" ]; then
    N=$FITS
fi

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/aflat" >> processing.sh
else
   ls $N | nice $BIN/aflat
fi
}

##########################################################################

function muniphot 
{
echo
echo -n "Images to photometry: (default=$FITS) "
read N

if [ -z "$N" ]; then
    N=$FITS
fi

if [ ! -f ./com.dat ]; then
   cat > com.dat <<EOF
attach
sky
find 1 1
photometry
sort ap n +4
EOF
fi

if [ ! -f ./mphot.opt ]; then
   cat > mphot.opt <<EOF
READ NOISE (ADU; 1 frame)  = 15
GAIN (e-/ADU; 1 frame)     = 2.3
LOW GOOD DATUM (in sigmas) = 7
HIGH GOOD DATUM (in ADU)   = 65000.0
FWHM OF OBJECT             = 1.5
THRESHOLD (in sigmas)      = 5.0
LS (LOW SHARPNESS CUTOFF)  = 0.2
HS (HIGH SHARPNESS CUTOFF) = 1.0
LR (LOW ROUNDNESS CUTOFF)  = -1.0
HR (HIGH ROUNDNESS CUTOFF) = 1.0
WATCH PROGRESS             = 0.0
FITTING RADIUS             = 2.0
PSF RADIUS                 = 15.0
VARIABLE PSF               = 0.0
FRACTIONAL-PIXEL EXPANSION = 0.0
ANALYTIC MODEL PSF         = 1
EXTRA PSF CLEANING PASSES  = 5
PERCENT ERROR (in %)       = 0.75
PROFILE ERROR (in %)       = 5
EOF
fi

if [ ! -f ./mphoto.opt ]; then
   cat > mphoto.opt <<EOF
A1  RADIUS OF APERTURE  1  = 2.0
A2  RADIUS OF APERTURE  2  = 2.7273
A3  RADIUS OF APERTURE  3  = 3.8182
A4  RADIUS OF APERTURE  4  = 5.2727
A5  RADIUS OF APERTURE  5  = 7.0909
A6  RADIUS OF APERTURE  6  = 9.2727
A7  RADIUS OF APERTURE  7  = 11.8182
A8  RADIUS OF APERTURE  8  = 14.7273
A9  RADIUS OF APERTURE  9  = 18.0
AA  RADIUS OF APERTURE 10  = 21.6364
AB  RADIUS OF APERTURE 11  = 25.6364
AC  RADIUS OF APERTURE 12  = 30.0
IS       INNER SKY RADIUS  = 20.0
OS       OUTER SKY RADIUS  = 30.0
EOF
fi

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/muniphot @ com=com.dat" >> processing.sh
else
   ls $N | nice $BIN/muniphot @ com=com.dat
fi
}

#########################################################################

function match 
{

M=`echo $FITS | awk '{ split($0,a,"."); print a[1]; }'`
M="$M.SRT"

echo
echo -n "Images to matching: (default=$M) "
read N

if [ -z "$N" ]; then
    N=$M
fi

echo -n "reference image: "
read D

if [ ! -f ./match.opt ]; then
   cat > match.opt <<EOF
Maximum read stars        = 10
Max. identification stars = 5
Sigma clipping factor     = 2.5
Watch progress            = 0
Blank lines               = 1
EOF
fi

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/munimatch @ ref=$D" >> processing.sh
else
   ls $N | nice $BIN/munimatch @ ref=$D
fi
}

##########################################################################

function munilist 
{
echo
echo -n "Images to list from: (default=$FITS) "
read N

if [ -z "$N" ]; then
    N=$FITS
fi

A=""

X=0
while test $X -ne 6 ; do

echo "1 ... output type, full"
echo "2 ... aperture No. (default=4)"
echo "3 ... default suffix (MAT)"
echo "4 ... star coordinates "
echo "5 ... variable star elements for phase output"
echo "6 ... continue"
echo -n "What? "
read X

case $X in
      1)   A="$A -of "; echo "full output...";; 
      2)   echo -n "aperture="; read B; A="$A column=$B";;
      3)   echo -n "suffix="; read B; A="$A suf=$B";;
      4)   echo -n "alpha,delta in degrees= "; read B; A="$A coo=$B";;
      5)   echo -n "epoch, period= "; read B; A="$A ele=$B";;
esac  

done

echo
echo -n "give list of star's lines separated by spaces: "
read X

if [ SIM -eq 1 ]; then
   echo "ls $N | nice $BIN/munilist @ $A $X" >> processing.sh
else
   ls $N | nice $BIN/munilist @ $A $X
fi
}

##########################################################################

function filter
{
echo 
echo -n "Filter set? [$FILTER] "
read -a an 

FILTER=${an[@]}
#echo $FILTER

#for A in $FILTER; do
#    echo $A
#done

}

##########################################################################
echo
echo "   M u n i p a c k     p h o t o m e t r y    g u i d e"
echo 

while true; do

    # main menu
    echo "-------------------------------------------------------------------"
    echo
    echo "1 ... Data conversion to FITS image format"
    echo "2 ... Dark frame subtraction"
    echo "3 ... Flat frame correction"
    echo "4 ... Mean of images (for example mean of darks)"
    echo "5 ... Create flat frame from scientific exposures"
    echo "6 ... Make photometry"
    echo "7 ... Recompute starts to reference image"
    echo "8 ... List stars from serie"
    echo "9 ... set filter"
    echo "q ... quit"
    echo
    echo -n "What? [$WHAT] "
    read WHAT

    if [ -z "$WHAT" ]; then
      WHAT=$WHATo
    fi

    WHATo=$WHAT
    case $WHAT in
      1)         konve;;
      2)         dark;;
      3)         flat;;
      4)         mean;;  
      5)         autoflat;;
      6)         muniphot;;
      7)         match;;
      8)         munilist;;
      9)         filter;;
      q)       exit 0;;
    esac  

done

exit 0
